package com.pactera.asmp.server.restfullapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pactera.asmp.server.common.constants.Constants;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.logprint.utils.StringUtils;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.entity.ScriptGroupSearchDTO;
import com.pactera.asmp.server.pojo.ScriptGroupInfo;
import com.pactera.asmp.server.pojo.ScriptInfo;
import com.pactera.asmp.server.service.ScriptGroupServcieImpl;
import com.pactera.asmp.server.service.ScriptInfoServcieImpl;
import com.pactera.asmp.server.utils.PageDataResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: tlsserver
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfullScriptGroupInfoApi
 * @Description: 脚本组存储
 * @Date 2020/06/01 11:05
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1")
public class RestfullScriptGroupInfoApi {

    private static final Logger logger = LoggerFactory
            .getLogger(RestfullScriptGroupInfoApi.class);

    @Autowired
    private ScriptGroupServcieImpl mScriptGroupServcieImpl;
    @Autowired
    private ScriptInfoServcieImpl mScriptInfoServcieImpl;

    @GetMapping(value = "/scriptgroup",  produces = "application/json;charset=UTF-8")
    public ResponseResult getScriptGroup(@RequestParam("scriptGroupSearchDTO") String scriptGroupSearchDTO, @RequestParam("status") boolean status) {
        logger.info("/api/v1/scriptgroup getScriptGroup scriptGroupSearchDTO:{}" ,scriptGroupSearchDTO);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        JSONObject result = (JSONObject) JSON.parse(scriptGroupSearchDTO);
        ScriptGroupSearchDTO data = JSON.toJavaObject(result, ScriptGroupSearchDTO.class);
        PageDataResult pdr;
        if (null == data.getPage() || 0 == data.getPage().intValue()) {
            data.setPage(1);
        }
        if (null == data.getLimit() ||0 == data.getLimit().intValue()) {
            data.setLimit(Integer.MAX_VALUE);
        }
        if(data.getPage().intValue() < 0 || data.getLimit().intValue() < 0) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("totals", 0);
            jsonObject.put("list", Lists.newArrayList());
            responseResult.setData(jsonObject);
            return responseResult;
        }
        if(status){
            pdr = mScriptGroupServcieImpl.selectScriptGroupInfo(data);
        }else {
            pdr = mScriptGroupServcieImpl.select(data);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totals", pdr.getTotals());
        jsonObject.put("list", pdr.getList());
        responseResult.setData(jsonObject);
        return responseResult;
    }


    @GetMapping(value = "/scriptgroup/{ids}", produces = "application/json;charset=UTF-8")
    public ResponseResult getScriptListByGroupId(@PathVariable("ids") List<Integer> groupIds) {
        logger.info("/api/v1/scriptgroup getScriptListByGroupId : groupId:{}" , groupIds );
        if(groupIds.isEmpty()){
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        List<ScriptInfo> datas = mScriptInfoServcieImpl.selectScriptByGroupIds(groupIds);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", datas);
        responseResult.setData(jsonObject);
        return responseResult;
    }

    @PostMapping(value = "/scriptgroup", produces = "application/json;charset=UTF-8")
    public ResponseResult addScriptGroup(@RequestBody ScriptGroupInfo scriptGroupInfo) {
        logger.debug("/api/v1/scriptgroup addScriptGroup ScriptGroupInfo :{}" , scriptGroupInfo);
        if (null == scriptGroupInfo) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtils.isEmpty(scriptGroupInfo.getName()) || StringUtils.isEmpty(scriptGroupInfo.getUserName())
                || scriptGroupInfo.getParentId() == null || scriptGroupInfo.getProjectId() == null) {
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        if(!scriptGroupInfo.getName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
            return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        Integer newGroupId= mScriptGroupServcieImpl.insert(scriptGroupInfo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId", newGroupId);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(jsonObject);
        return responseResult;
    }


    @PutMapping(value = "/scriptgroup", produces = "application/json;charset=UTF-8")
    public ResponseResult updateScriptGroup(@RequestBody ScriptGroupInfo scriptGroupInfo) {
        logger.info("/api/v1/scriptgroup updateScriptGroup  :{}" , scriptGroupInfo);
        if (scriptGroupInfo == null || scriptGroupInfo.getId() == 0 || StringUtils.isEmpty(scriptGroupInfo.getUserName()) || StringUtils.isEmpty(scriptGroupInfo.getName())) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if(!scriptGroupInfo.getName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
            return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        return mScriptGroupServcieImpl.update(scriptGroupInfo) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
    }

    @DeleteMapping(value = "/scriptgroup",  produces = "application/json;charset=UTF-8")
    public ResponseResult deleteScriptGroup(@RequestParam(name = "id") Integer id, @RequestParam(name = "groupId") Integer groupId) {
        logger.info("/api/v1/scriptgroupgroup deleteScriptGroup id :{} ,groupId:{}",id,groupId);
        ResponseResult responseResult = null;
        if ((groupId == null || groupId == 0) && id == null) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        if (groupId == null || groupId == 0) {
            responseResult = mScriptGroupServcieImpl.deleteGroupAndScriptRelByGID(ids) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
        }

        if (groupId != null && groupId != 0 && id != null && !ids.isEmpty()) {
            responseResult = mScriptGroupServcieImpl.deleteInGroupThisScriptRel(groupId, ids) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
        }
        return responseResult;
    }


}
